<template>
    <div id="version-edit">
        <el-form :inline="false" label-width="80px" :model="formData" ref="formData" :rules="validatorRules">
            <el-form-item label="版本号"  prop="version">
                <el-input v-model="formData.version" placeholder="请输入版本号"></el-input>
            </el-form-item>
			<el-form-item label="文件上传"  prop="file" class="file-item">
			    <el-upload
			        action=""
			        class="file-upload"
			        accept=".apk"
			        :auto-upload="false"
			        :file-list="fileList"
			        :multiple="false"
			        :limit="1"
			        :on-remove="onRemove"
			        :on-exceed="alertMore"
			        :on-change="onChange"
			    >
			        <el-button size="mini" type="primary">点击上传</el-button>
			        <div slot="tip" class="tip">只能上传apk文件</div>
			    </el-upload>
			</el-form-item>
			<el-form-item label="强制更新"  prop="type">
			    <el-radio v-model="formData.type" label="0">是</el-radio>
			    <el-radio v-model="formData.type" label="1">否</el-radio>
			</el-form-item>
            <el-form-item label="更新描述"  prop="remark" class="textarea">
			    <el-input  type="textarea"  v-model="formData.remark" maxlength="200" show-word-limit autosize placeholder="不超200字"></el-input>
            </el-form-item>
        </el-form>
        <div class="button-bar">
            <el-button  @click="onBack">取消</el-button>
            <el-button type="primary"  @click="onSave" :loading="btnLoading">保存</el-button>
        </div>
    </div>
</template>
<script>
export default {
    props:{
        initData:{
            type:Object,
            default:{},
            require:false,
        }
    },
    data(){
        return{
            formData:{
                id:'',
                version:'',
                remark:'',
                type:'1',
				multipartFile:""
            },
            validatorRules:{
                version:[
                    {required: true, message: '请输入', trigger: 'change'},
                    {validator:this.checkVersion, trigger: 'change'},
                ],
                
            },
            fileList:[],
			btnLoading:false,

        }
    },
    watch:{
        initData(newVal){
            if(newVal){
                this.formData=newVal
                this.formData.multipartFile=""
            }
        },
       
    },
    methods:{
        onSave(){
            var isValidate=false
            this.$refs.formData.validate((bool)=>{
                if(bool){
                    isValidate=true
                }
            })
            if(!isValidate){return}
           if(this.formData.id==="" && this.formData.multipartFile==""){
               this.$message.warning("请上传文件")
               return false
           }
           // ------------------
           var formdata=new FormData()
           for(var key in this.formData){
               formdata.append(key,this.formData[key])
           }
           let submitData=formdata
		   this.btnLoading=true
            this.$http.addVersion(submitData)
            .then((res)=>{
                this.$message.success("保存成功!");
               this.onReset()
			   this.$emit("close")
			   this.btnLoading=false
            }).catch((err)=>{
				this.btnLoading=false
                throw(err)
            })
        },

        onReset(){
            this.$refs.formData.resetFields()
            this.$emit("refresh")
        },

        onBack(){
            this.$emit("close")
            this.onReset()
        },
        onChange(file, fileList){
            var bool=this.checkFile(file.raw)
            if(bool){
                this.formData.multipartFile=file.raw
                this.fileList=fileList
            }else{
                this.formData.multipartFile=""
                this.fileList=[]
            }
           // this.formData.file=file.raw
            //this.fileList=fileList
                
        },
        onRemove(file, fileList){
            this.fileList=[]
        },

        alertMore(){
            this.$message.warning("只能上传一个")
        },

        checkFile(file){
            var reg=/(.apk)/g
            if(!file.name.match(reg)){
                this.$message.warning("文件格式不支持,请重新上传!")
                return false
            }else{
                return true
            }
        },

        checkVersion(rule, value, callback){
			// var matchVal=value.match(/\d+(\.\d+){0,2}/);
			// var bool=matchVal==null || matchVal[0]=="" || matchVal[0]!=value
   //          if(bool){
   //              callback(new Error('版本号格式不正确!'));
   //          }else {
   //              callback();
   //          }
			if(value && value.length>0 && !value.match(/^\d*$/g)){
			    callback(new Error('只能输入整数数字!'));
			}else{
			    callback()
			}
        },

       


    }
}
</script>
<style scoped lang='scss'>
#version-edit{
	flex:1;
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
    .button-bar{
        padding: 10px 30px;
        text-align:center;
    }
    /deep/ .el-form{
        flex: 1;
        overflow: auto;
        padding:10px 30px;
        .el-textarea .textarea{
            min-height:100px;
            resize:vertical;
        }
        .file-item .el-form-item__label:before{
            content: '*';
            color: #F56C6C;
            margin-right: 4px;
        }
        .file-upload{
            .tip{
                display:inline-block;
                margin-left:10px;
                vertical-align:middle;
            }
        }
    }
}



</style>




